<?php
	
	//$userid = $_userid;
	$_userid = $_userid == 1 && $userid  ? $userid : $_userid; // 管理员编辑其他会员信息权限
	

	if(!isset($action)) $action = 'manage';
	require_once 'form.class.php';
	require_once 'includes/yellowpage.class.php';
	$yellowpage = new yellowpage();
	$yellowpage->set_model('product');
	$yellowpage->set_userid($_userid);
	foreach($MODEL AS $modelid=>$value)
	{
		if($value['modeltype']==9 && $value['tablename'] == 'product') break;
	}
	$yellowpage->modelid = $modelid;

	
	switch($action){
		
		case 'show':
			require_once 'includes/yellowpage.class.php';
			require_once 'includes/company.class.php';
			$company = new company();
			$productid = intval($id);
			if(!$productid) exit('非法参数');
			$yellowpage->hits($id);
			cache_page_start();
			$rs = $yellowpage->get($productid);
			if($rs['status'] != 99) showmessage('信息正在审核中...');
			$head['keywords'] .= $rs['keywords'].'_产品';
			$head['description'] .= $rs['title'].'_产品'.'_'.$SETTING['sitename'];
			$head['title'] .= $rs['title'].'_产品'.'_'.$SETTING['sitename'];
			$c = $company->get($rs['userid']);
			if(intval($readproid))
			{
				$readproid = $productid.','.$readproid;
				$tmp = explode(",",$readproid);
				$tmp = array_unique($tmp);
				while(count($tmp) > 10)array_pop($tmp);
				$readproid = implode(",",$tmp);
			}
			else $readproid = $productid;
			set_cookie('readproid',$readproid,time()+3600*365*24); 
			include template('product_show.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
		
		case 'search':
			$head['keywords'] .= '产品搜索';
			$head['description'] .= '产品搜索'.'_'.$SETTING['sitename'];
			$head['title'] .= '产品搜索'.'_'.$SETTING['sitename'];
			$CAT = subcat(0,'yellowpage'); 
			include template('product_search.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
		
		case 'searchlist':
			$head['keywords'] .= '产品搜索结果';
			$head['description'] .= '产品搜索结果'.'_'.$SETTING['sitename'];
			$head['title'] .= '产品搜索结果'.'_'.$SETTING['sitename'];
			$page = $page?$page:1;
			$catid = intval($catid);
			$q = addslashes(htmlspecialchars($q));
			$areaname = addslashes(htmlspecialchars($areaname));
			$lprice = intval($lprice);
			$hprice = intval($hprice);
			if($catid)
			{
				$c= new company();
				$r = $yellowpage->get_yp_arrchildid($catid);
				if($r['arrchildid'])$r['arrchildid'] = $catid.','.$r['arrchildid'];
				else $r['arrchildid'] = $catid;
				$where .= "p.catid IN ($catid) AND ";
			}
			if($lprice)$where .= "p.price >= '{$lprice}' AND ";
			if($hprice)$where .= "p.price <= '{$hprice}' AND ";
			if($areaname)$where .= "c.areaname = '{$areaname}' AND ";
			if($q)$where .= "p.title LIKE '%{$q}%' AND ";
			$where .= "c.userid = p.userid"; 
			include template('product_searchlist.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break; 
		
	
	case 'add':
		
		header("Cache-control: private");
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit)
		{
			$secondcatid = intval($secondcatid);
			$thirdcatid = intval($thirdcatid);
			if($thirdcatid)
			{
				$info['catid'] = $thirdcatid;
			}
			elseif($secondcatid)
			{
				$info['catid'] = $secondcatid;
			}
			else
			{
				$info['catid'] = $catid;
			}
			if(!$info['catid']) showmessage('栏目参数错误');
			/*if(in_array($_groupid,$M['add_check']))
			{
				$info['status'] = 99;
			}
			else
			{
				$info['status'] = 1;
			}*/
			$info['status'] = 99;
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, $info['catid']);
			$contentid = $yellowpage->add($info) ;
			
			// by aming 
			/*$url = "?module=$module&action=web&userid=$_userid&category=product&id=$contentid";
			$db->query("UPDATE `".DB_PRE."yellowpage_product` SET `url`='$url' WHERE `id`=$contentid");*/
			showmessage('发布成功！', url("?module=$module&file=$file&action=$action&userid=$_userid"));
		}
		else
		{
			if($M['ischeck'] && $company_user_infos['status'] == 0) showmessage('您的公司正在审核当中...','goback');
			if($company_user_infos['endtime'] && $company_user_infos['endtime']<TIME) showmessage('您的服务截至日期已到，请续费...','goback');
			foreach($MODEL AS $modelid=>$value)
			{
				if($value['modeltype']==9 && $value['tablename'] == 'product') break;
			}
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $content_form->get($data);
		}
		include template('product_add.html','yellowpage');
		break;
	
	case 'manage':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		$where = '1';
		$infos = $yellowpage->listinfo($where, '`id` DESC', $page, 20, 1);
		$pages = $yellowpage->pages;
		include template('product_manage.html','yellowpage');
		break;
	
	case 'edit':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		// common
		if(!isset($action)) $action = 'manage';
		require_once 'form.class.php';
		
		foreach($MODEL AS $modelid=>$value)
		{
			if($value['modeltype']==9 && $value['tablename'] == 'product') break;
		}
		$yellowpage->modelid = $modelid;
		// common end
		
		$id = intval($id);
		if($dosubmit)
		{
			$secondcatid = intval($secondcatid);
			$thirdcatid = intval($thirdcatid);
			if($thirdcatid)
			{
				$info['catid'] = $thirdcatid;
			}
			elseif($secondcatid)
			{
				$info['catid'] = $secondcatid;
			}
			else
			{
				$info['catid'] = $catid;
			}
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, $catid);
			$yellowpage->edit($id, $info);
			
			// by aming 
			//$url = "?module=$module&action=web&userid=$_userid&category=product&id=$id";
			//$db->query("UPDATE `".DB_PRE."yellowpage_product` SET `url`='$url' WHERE `id`=$id");
			showmessage('修改成功！', $forward);
		}
		else
		{
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data = $yellowpage->get($id);
			$forminfos = $content_form->get($data);
		}
		include template('product_edit.html','yellowpage');
		break;
		
	case 'delete':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));  
		$id = intval($id);
		$yellowpage->delete($id);
		showmessage('删除成功！', $forward);
		include template('product_manage.html','yellowpage');
		break;
		
	default:
		$catid = intval($catid);
		$head['keywords'] .= '产品';
		$head['description'] .= '产品'.'_'.$SETTING['sitename'];
		$head['title'] .= '产品'.'_'.$SETTING['sitename'];
		$CAT = subcat(0,'yellowpage');
		if($catid)
		{
			if($child == 1) $arrchildid = subcat($catid,'yellowpage');
		}
		$view_type = max(intval($view_type), 1);
		$page = $page ? $page : 1;
		$pagesize = $pagesize ? $pagesize : 20;
		$where = " WHERE p.userid=c.userid AND p.status=99";
		if($catid)
		{
			if($CATEGORY[$catid]['arrchildid'])
				$where .= " AND p.catid IN (".$CATEGORY[$catid]['arrchildid'].")";
			else
				$where .= " AND p.catid='$catid'";
		}
		if($areaname)
		{
			$areaname = htmlspecialchars($areaname);
			if(strpos(',',$areaname)===false)
			{
				$where .= " AND c.areaname='$areaname'";
			}
			else
			{
				$where .= " AND c.areaname IN ($areaname)";
			}
			$areaname = urlencode($areaname);
		}
		else
		{
			$areaname = 0;
		}
		if(isset($order))
		{
			$order = intval($order);
			$orderby = $order ? 'ASC' : 'DESC';
			$sql = "SELECT * FROM `".DB_PRE."yellowpage_product` p,`".DB_PRE."member_company` c $where ORDER BY p.price {$orderby}";
		}
		else
		{
			$sql = "SELECT * FROM `".DB_PRE."yellowpage_product` p,`".DB_PRE."member_company` c $where ORDER BY p.id DESC";
		} 
		if($M['enable_rewrite'])
		{
			$urlrule = "$M[url]product-list-$view_type-$catid-$pagesize--$areaname--$order.html|$M[url]product-list-$view_type-$catid-$pagesize--$areaname--$order-\$page.html";
		}
		else
		{
			$urlrule = "$M[url]product.php?view_type=$view_type&catid=$catid&pagesize=$pagesize&areaname=$areaname&order=$order|$M[url]product.php?view_type=$view_type&catid=$catid&pagesize=$pagesize&areaname=$areaname&order=$order&page=\$page";
		}
		include template('product.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;	
}
?>